import React from 'react';
import { history } from 'umi'
import { PageContainer } from '@/molecules';
import authHelper from '@/utils/auth/helper';
import Informal from './Informal/Informal';
import EntrySettingIndex from './EntrySetting/EntrySettingIndex';
import LeaveSetting from './LeaveSetting';

const { checkCode } = authHelper;

// const componentA = <div>转正设置</div>;
// const componentB = <div>调岗设置</div>;
// const componentC = <div>离职设置</div>;

const tabList = [
    {
        key: '1',
        tab: '入职设置',
        code: 'office:turnover:setting:entry',
        component: <EntrySettingIndex />
    },
    {
        key: '2',
        tab: '转正设置',
        code: 'office:turnover:setting:informal',
        component: <Informal />
    },
    // {
    //     key: '3',
    //     tab: '调岗设置',
    //     code: true,
    //     component: componentB
    // },
    {
        key: '4',
        tab: '离职设置',
        code: true,
        component: <LeaveSetting />
    }
];

const Setting = ({ location }) => {
    const { query } = location;
    const filterList = tabList.filter(item => checkCode(item.code)) || [];
    const [activeKey, setActiveKey] = React.useState(query?.activeKey || filterList[0]?.key || '1');

    const handleTabChange = key => {
        setActiveKey(key);
        history.push({
            query: {
                activeKey: key
            }
        });
    };

    React.useEffect(() => {
        const key = query?.activeKey || filterList[0]?.key || '1';
        setActiveKey(key);
    }, [query]);

    React.useEffect(() => {
        if (filterList && filterList.length > 0) {
            if (!filterList.find(item => item.key === activeKey)) {
                setActiveKey(filterList[0]?.key || '');
            }
        }
    }, [JSON.stringify(filterList)]);

    return (
        <PageContainer tabList={filterList} activeTabKey={activeKey} onTabChange={handleTabChange} id="turnoverSetting">
            {filterList?.find(item => item.key === activeKey)?.component}
        </PageContainer>
    );
};

export default Setting;
